﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;

namespace WXYW.BLL
{
    public class SpecialSettingService : RepositoryBase
    {
        public int DeleteRMA(string applicationCode)
        {
            ApplicationForm applicationToDel = GetEntities<ApplicationForm>().Where(a => a.ApplicationCode.Equals(applicationCode)).FirstOrDefault();

            if (applicationToDel != null)
            {
                if (!applicationToDel.FeedbackReference.IsLoaded)
                {
                    applicationToDel.FeedbackReference.Load();
                }
                if (applicationToDel.Feedback != null)
                {
                    DeleteEntities<Feedback>(f => f.FeedbackID == applicationToDel.Feedback.FeedbackID, Guid.Empty);
                }

                DeleteEntities<ApplicationProductHistory>(p => p.ApplicationFormHistory.RunningNumber.Equals(applicationToDel.RunningNumber), Guid.Empty);
                DeleteEntities<ApplicationFormHistory>(p => p.RunningNumber.Equals(applicationToDel.RunningNumber), Guid.Empty);
                DeleteEntities<ApplicationPicture>(p => p.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<Gathering>(g => g.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<QuatationForm>(p => p.ApplicationProduct.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<UpgradeForm>(p => p.ApplicationProduct.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<ReturnToFactoryQuatation>(r => r.ReturnToFactoryForm.ApplicationProduct.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<ReturnToFactoryForm>(r => r.ApplicationProduct.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<SparePartApplyForm>(s => s.ApplicationProduct.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<SpecialServiceProduct>(s => s.ApplicationProduct.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<ApplicationProduct>(p => p.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<ConfirmReceiveProduct>(p => p.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<ProblemAnalyze>(p => p.ApplicationProduct.FirstOrDefault().ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<TFSQContent>(t => t.SpecialServiceForm.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<SpecialServiceForm>(s => s.ApplicationForm.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<ApplicationForm>(p => p.ApplicationCode.Equals(applicationCode), Guid.Empty);
                DeleteEntities<RequisitionForm>(r => r.ApplicationProduct.Where(p => p.ApplicationForm.ApplicationCode.Equals(applicationCode)).Count() > 0, Guid.Empty);

                return 1;
            }
            else
            {
                return 0;
            }
        }

        public string GetStatusOfApplication(string applicationCode)
        {
            ApplicationForm application = GetEntities<ApplicationForm>().Where(a => a.ApplicationCode.Equals(applicationCode)).FirstOrDefault();
            if (application != null)
            {
                return application.Status;
            }
            else
            {
                return string.Format("找不到{0}, 请确认单号是否正确！", applicationCode);
            }
        }

        /// <summary>
        /// 2010-06-02
        /// 陈璞  22:31:28
        /// DOA/RMA互换 是既可以从DOA变为RMA 也可以从RMA变为DOA 还是只需要从DOA变为RMA
        /// 朱经理  22:32:41
        /// 互换
        /// 朱经理  22:32:49
        /// 都可以转的意思
        /// </summary>
        /// <param name="applicationCode"></param>
        /// <returns></returns>
        public int ExchangeRMADOA(string applicationCode, ref string newApplicationCode)
        {
            int retCode = 1;
            ApplicationForm application = GetEntities<ApplicationForm>().Where(a => a.ApplicationCode.Equals(applicationCode)).FirstOrDefault();
            if (application != null)
            {
                ApplicationService applicationService = new ApplicationService();
                if (!application.ServiceStationReference.IsLoaded)
                {
                    application.ServiceStationReference.Load();
                }
                // RMA-> DOA
                if (application.ApplicationCode.StartsWith("R"))
                {
                    newApplicationCode = RunningNumberService.GenerateDOMNumber(application.ServiceStation.ForShort);
                    application.ApplicationCode = newApplicationCode;
                    application.ApplicationType = "D";
                    applicationService.UpdateApplication(application);                    
                }
                // DOA-> RMA
                else if (application.ApplicationCode.StartsWith("D"))
                {
                    newApplicationCode = RunningNumberService.GenerateRMANumber(application.ServiceStation.ForShort);
                    application.ApplicationCode = newApplicationCode;
                    application.ApplicationType = "A";
                    applicationService.UpdateApplication(application);                    
                }
                // 不合法的RMA/DOA单号类型
                else
                {
                    retCode = -1;
                }
            }
            else
            {
                retCode = 0;
            }

            return retCode;
        }

        public int ChangeStatusOfApplication(string applicationCode, string status)
        {
            int retCode = 1;
            ApplicationForm application = GetEntities<ApplicationForm>().Where(a => a.ApplicationCode.Equals(applicationCode)).FirstOrDefault();
            if (application != null)
            {
                if (!application.ApplicationProduct.IsLoaded)
                {
                    application.ApplicationProduct.Load();
                }
                #region old code



                #endregion
                //switch (status)
                //{
                //    case "待派单":
                //        application.Status = "待派单";
                //        if (!application.FeedbackReference.IsLoaded)
                //        {
                //            application.FeedbackReference.Load();
                //        }
                //        application.Feedback = null;
                //        if (!application.ServiceStationReference.IsLoaded)
                //        {
                //            application.ServiceStationReference.Load();
                //        }
                //        application.SendWay = null;
                //        application.CompleteDateTime = null;
                //        application.SendDateTime = null;
                //        application.NortificationDatetime = null;
                //        application.SendBackDateTime = null;
                //        application.ClientReceiveDateTime = null;
                //        application.QuatationDatetime = null;
                //        application.DistributeDateTime = null;
                //        application.KPDateTime = null;
                //        application.QuoteBy = null;

                //        foreach (ApplicationProduct product in application.ApplicationProduct)
                //        {
                //            if (product.Status.Equals("待修"))
                //            {
                //                product.RepairAccessory = null;
                //                if (!product.UserReference.IsLoaded)
                //                {
                //                    product.UserReference.Load();
                //                }
                //                product.User = null;
                //                product.Cost = null;
                //                product.ReasonOfCost = null;
                //                product.ReasonOfNotRepair = null;
                //                product.ReceiveRemark = null;
                //                product.Remark = null;
                //                product.Status = null;
                //                product.RepairCompleteDateTime = null;
                //                product.IsQuotePrice = null;
                //                product.IsDecideToRepair = null;
                //                product.ReasonToProblem = null;

                //                product.DistributeDateTime = null;
                //                product.ProductRepairType = null;
                //                product.ProductPurchaseDateTime = null;
                //            }
                //        }
                //        break;
                //    case "等待RMA单号确认":
                //        application.Status = "等待RMA单号确认";
                //        if (!application.FeedbackReference.IsLoaded)
                //        {
                //            application.FeedbackReference.Load();
                //        }
                //        application.Feedback = null;
                //        if (!application.ServiceStationReference.IsLoaded)
                //        {
                //            application.ServiceStationReference.Load();
                //        }
                //        application.ServiceStation = null;
                //        application.ApplicationCode = null;
                //        application.SendWay = null;
                //        application.CompleteDateTime = null;
                //        application.SendDateTime = null;
                //        application.ReceiveDateTime = null;
                //        application.NortificationDatetime = null;
                //        application.SendBackDateTime = null;
                //        application.ClientReceiveDateTime = null;
                //        application.QuatationDatetime = null;
                //        application.DistributeDateTime = null;
                //        application.RMANOGenerateDateTime = null;
                //        application.KPDateTime = null;
                //        application.QuoteBy = null;

                //        foreach (ApplicationProduct product in application.ApplicationProduct)
                //        {
                //            product.RepairAccessory = null;
                //            if (!product.UserReference.IsLoaded)
                //            {
                //                product.UserReference.Load();
                //            }
                //            product.User = null;
                //            product.Cost = null;
                //            product.ReasonOfCost = null;
                //            product.ReasonOfNotRepair = null;
                //            product.ReceiveRemark = null;
                //            product.Remark = null;
                //            product.Status = null;
                //            product.RepairCompleteDateTime = null;
                //            product.IsQuotePrice = null;
                //            product.IsDecideToRepair = null;
                //            product.ReasonToProblem = null;
                //        }
                //        break;
                //    case "等待DOA单号确认":
                        
                //        break;
                //    case "维修完毕待发货":
                //        foreach (ApplicationProduct product in application.ApplicationProduct)
                //        {
                //            if (!product.NoticeClientDateTime.HasValue && !product.SendBackTime.HasValue && !product.ClientReceiveDateTime.HasValue &&
                //                !product.Status.Equals("维修完毕待发货"))
                //            {
                //                if (!product.RepairCompleteDateTime.HasValue)
                //                {
                //                    product.RepairCompleteDateTime = DateTime.Now;
                //                }
                //                product.Status = "维修完毕待发货";
                //                UpdateEntity(product);
                //            }
                //        }
                //        break;
                //    case "待修":
                //        foreach (ApplicationProduct product in application.ApplicationProduct)
                //        {
                //            product.Status = "待修";
                //            product.RepairCompleteDateTime = null;
                //            product.ReplaceProductType = string.Empty;
                //            product.ReplaceProductVersion = string.Empty;
                //            product.SendBackTime = null;
                //            product.SendWay = string.Empty;
                //            product.SPCode = string.Empty;
                //            product.AccessoryPlace = string.Empty;
                //            product.BLXX = string.Empty;
                //            product.ChangeType = string.Empty;
                //            product.ClientReceiveDateTime = null;
                //            product.ConfirmQuotationDateTime = null;
                //            product.Cost = null;
                //            product.IsDecideToRepair = null;
                //            product.IsDiaoBo = null;
                //            product.IsQuotePrice = null;
                //            product.KPType = string.Empty;
                //            product.NoticeClientDateTime = null;
                //            product.QuotationDateTime = null;
                //            product.ReasonOfCost = string.Empty;
                //            product.ReasonOfNotRepair = string.Empty;
                //            product.ReasonToProblem = string.Empty;
                //            product.RepairAccessory = string.Empty;
                //            UpdateEntity(product);

                //            if (!product.ProblemAnalyzeReference.IsLoaded)
                //            {
                //                product.ProblemAnalyzeReference.Load();
                //            }
                //            if (product.ProblemAnalyze != null)
                //            {
                //                DeleteEntity<ProblemAnalyze>(p => p.ID == product.ProblemAnalyze.ID, Guid.Empty);
                //            }

                //            if (!product.RequisitionFormReference.IsLoaded)
                //            {
                //                product.RequisitionFormReference.Load();
                //            }
                //            if (product.RequisitionForm != null)
                //            {
                //                DeleteEntity<RequisitionForm>(r => r.ID == product.RequisitionForm.ID, Guid.Empty);
                //            }

                //            if (!product.ReturnToFactoryForm.IsLoaded)
                //            {
                //                product.ReturnToFactoryForm.Load();
                //            }
                //            if (product.ReturnToFactoryForm != null && product.ReturnToFactoryForm.Count > 0)
                //            {
                //                DeleteEntity<ReturnToFactoryForm>(r => r.ApplicationProduct.ApplicationProductID == product.ApplicationProductID, Guid.Empty);
                //            }

                //            DeleteEntities<QuatationForm>(q => q.ApplicationProduct.ApplicationProductID == product.ApplicationProductID, Guid.Empty);
                //        }
                //        break;
                //    case "报价确认中":
                //        foreach (ApplicationProduct product in application.ApplicationProduct)
                //        {
                //            if (!product.ReturnToFactoryForm.IsLoaded)
                //            {
                //                product.ReturnToFactoryForm.Load();
                //            }
                //            if (!product.QuatationForm.IsLoaded)
                //            {
                //                product.QuatationForm.Load();
                //            }
                //            if (product.Status.Equals("已确认-待修"))
                //            {
                //                if (product.ReturnToFactoryForm != null && product.ReturnToFactoryForm.Count > 0)
                //                {
                //                    product.Status = "返厂-待客户报价确认";
                //                }
                //                else if (product.QuatationForm.Count > 0)
                //                {
                //                    product.Status = "报价确认中";
                //                }
                //            }
                //            else if (product.QuatationForm.Count > 0)
                //            {
                //                product.Status = "报价确认中";
                //            }
                //            product.ConfirmQuotationDateTime = null;
                //            product.IsDecideToRepair = null;
                //            product.RepairCompleteDateTime = null;
                //            product.SendBackTime = null;
                //            product.SendWay = null;                            
                //        }
                //        break;
                //}
                UpdateEntity(application);
            }
            else
            {
                retCode = 0;
            }

            return retCode;
        }

        public int ChangeStatusOfApplicationProduct(string applicationCode, string productSN, string status)
        {
            int retCode = 1;
            ApplicationProduct applicationProduct = GetEntities<ApplicationProduct>().Where(a => a.ApplicationForm.ApplicationCode.Equals(applicationCode) && a.ProductSerialNumber == productSN).FirstOrDefault();
            if (applicationProduct != null)
            {
                applicationProduct.Status = status;
                
                UpdateEntity(applicationProduct);
            }
            else
            {
                retCode = 0;
            }

            return retCode;
        }

        public int ChangeEngineer(string applicationCode, string oriSerialNO, int engineerID)
        {
            int retCode = 1;
            ApplicationForm application = GetEntities<ApplicationForm>().Where(a => a.ApplicationCode.Equals(applicationCode)).FirstOrDefault();
            if (application != null)
            {
                if (!application.ApplicationProduct.IsLoaded)
                {
                    application.ApplicationProduct.Load();
                }
                if (string.IsNullOrEmpty(oriSerialNO))
                {
                    foreach (ApplicationProduct product in application.ApplicationProduct)
                    {
                        product.User = GetEntities<User>().Where(u => u.UserID == engineerID).FirstOrDefault();
                    }
                    UpdateEntity(application);
                }
                else
                {
                    IEnumerable<ApplicationProduct> products = application.ApplicationProduct.Where(p => p.ProductSerialNumber.Equals(oriSerialNO));
                    if (products != null && products.Count() > 0)
                    {
                        ApplicationProduct product = products.FirstOrDefault();
                        product.User = GetEntities<User>().Where(u => u.UserID == engineerID).FirstOrDefault();
                        UpdateEntity(product);
                    }
                    else
                    {
                        retCode = -2;
                    }
                }
            }
            else
            {
                retCode = -1;
            }
            return retCode;
        }

        public int ChangeSN(string applicationCode, string originalSerialNO, string newSerialNO)
        {
            int retCode = 1;
            ApplicationForm application = GetEntities<ApplicationForm>().Where(a => a.ApplicationCode.Equals(applicationCode)).FirstOrDefault();
            if (!application.ApplicationProduct.IsLoaded)
            {
                application.ApplicationProduct.Load();
            }
            if (application != null)
            {
                ApplicationProduct product = application.ApplicationProduct.Where(p => p.ProductSerialNumber.Equals(originalSerialNO)).FirstOrDefault();
                if (product != null)
                {
                    product.ProductSerialNumber = newSerialNO;
                    UpdateEntity(product);
                }
                else
                {
                    retCode = -2;
                }
            }
            else
            {
                retCode = -1;
            }
            return retCode;
        }

        public int ChangeBJXXStatus(string applicationCode, string status)
        {
            int retCode = 1;
            ApplicationForm application = GetEntities<ApplicationForm>().Where(a => a.ApplicationCode.Equals(applicationCode)).FirstOrDefault();
            if (application != null)
            {
                application.Status = status;
                UpdateEntity(application);
            }
            else
            {
                retCode = -1;
            }

            return retCode;
        }

        public int ChangeBJApplyStatus(string applicationCode, string status)
        {
            int retCode = 1;
            SparePartApplyForm application = GetEntities<SparePartApplyForm>().Where(a => a.Code.Equals(applicationCode)).FirstOrDefault();
            if (application != null)
            {
                switch (status)
                {
                    case "备件-待归还确认":
                        if (application.Status.Equals("备件-已归还"))
                        {
                            application.ReceiveReturnTime = null;
                        }
                        else
                        {
                            retCode = 9;
                        }

                        break;
                    case "备件-待返还":
                        if (application.Status.Equals("备件-已归还") || application.Status.Equals("备件-待归还确认"))
                        {
                            application.ReceiveReturnTime = null;

                            application.ReturnProductType = null;
                            application.ReturnProductSerialNumber = null;
                            application.SendReturnTime = null;
                        }
                        else
                        {
                            retCode = 9;
                        }

                        break;
                    case "备件-待收货确认":
                        if (application.Status.Equals("备件-已归还") || application.Status.Equals("备件-待归还确认") || application.Status.Equals("备件-待返还"))
                        {
                            application.ReceiveReturnTime = null;

                            application.ReturnProductType = null;
                            application.ReturnProductSerialNumber = null;
                            application.SendReturnTime = null;

                            application.ReceiveTime = null;
                            if (!application.ApplicationProductReference.IsLoaded)
                            {
                                application.ApplicationProductReference.Load();
                            }
                            application.ApplicationProduct.Status = "备件申请中";
                        }
                        else
                        {
                            retCode = 9;
                        }

                        break;
                    case "备件-不同意":
                        if (application.Status.Equals("备件-已归还") || application.Status.Equals("备件-待归还确认")
                            || application.Status.Equals("备件-待返还") || application.Status.Equals("备件-待收货确认"))
                        {
                            application.ReceiveReturnTime = null;

                            application.ReturnProductType = null;
                            application.ReturnProductSerialNumber = null;
                            application.SendReturnTime = null;

                            application.ReceiveTime = null;
                            if (!application.ApplicationProductReference.IsLoaded)
                            {
                                application.ApplicationProductReference.Load();
                            }
                            application.ApplicationProduct.Status = "待修";

                            application.SendTime = null;
                            application.SendProductSerialNumber = null;
                            application.SendPlace = null;
                            application.DealWithTime = DateTime.Now; // 不同意有处理时间
                        }
                        else
                        {
                            retCode = 9;
                        }

                        break;
                    case "备件-同意待发货":
                        if (application.Status.Equals("备件-已归还") || application.Status.Equals("备件-待归还确认")
                            || application.Status.Equals("备件-待返还") || application.Status.Equals("备件-待收货确认"))
                        {
                            application.ReceiveReturnTime = null;

                            application.ReturnProductType = null;
                            application.ReturnProductSerialNumber = null;
                            application.SendReturnTime = null;

                            application.ReceiveTime = null;
                            if (!application.ApplicationProductReference.IsLoaded)
                            {
                                application.ApplicationProductReference.Load();
                            }
                            application.ApplicationProduct.Status = "备件申请中";

                            application.SendTime = null;
                            application.SendProductSerialNumber = null;
                            application.SendPlace = null;
                        }
                        else
                        {
                            retCode = 9;
                        }

                        break;
                    case "备件-待审批":
                        if (application.Status.Equals("备件-已归还") || application.Status.Equals("备件-待归还确认") || application.Status.Equals("备件-待返还")
                            || application.Status.Equals("备件-待收货确认") || application.Status.Equals("备件-不同意") || application.Status.Equals("备件-同意待发货"))
                        {
                            application.ReceiveReturnTime = null;

                            application.ReturnProductType = null;
                            application.ReturnProductSerialNumber = null;
                            application.SendReturnTime = null;

                            application.ReceiveTime = null;
                            if (!application.ApplicationProductReference.IsLoaded)
                            {
                                application.ApplicationProductReference.Load();
                            }
                            application.ApplicationProduct.Status = "备件申请中";

                            application.SendTime = null;
                            application.SendProductSerialNumber = null;
                            application.SendPlace = null;

                            application.DeclineReason = null;
                            application.DealWithTime = null;
                        }
                        else
                        {
                            retCode = 9;
                        }

                        break;
                }

                if (9 != retCode) {
                    application.Status = status;
                    UpdateEntity(application);
                }
            }
            else
            {
                retCode = -1;
            }

            return retCode;
        }
    }
}

